Page({
    onLoad: function (options) {
        var _id = options._id;
        if (_id) {
            this.setData({
                _id: _id
            });
        }
        this.loadData({
            _id: _id
        });
    },
    save: function (e) {
        // 添加或是更新
        var content = e.detail.value.content;
        if (this.data._id) {
            wx.request({
                method: 'POST',
                url: 'http://node.it577.net:5001/update',
                data: {
                    _id: this.data._id,
                    content: content
                },
                success: res => {
                    wx.showModal({
                        title: res.data.msg,
                        showCancel: false,
                        success: () => {
                            wx.navigateBack();
                        }
                    });
                }
            });
        } else {
            wx.request({
                method: 'POST',
                data: {
                    content: content
                },
                url: 'http://node.it577.net:5001/save',
                success: (res) => {
                    wx.showModal({
                        title: res.data.msg,
                        showCancel: false,
                        success: () => {
                            wx.navigateBack();
                        }
                    });
                }
            });
        }
    },
    loadData: function (condition) {
        wx.request({
            method: 'GET',
            url: 'http://node.it577.net:5001/read',
            data: condition,
            success: (res) => {
                this.setData({
                    todo: res.data
                });
            }
        });
    }
});